package com.hxd.charitysystem.ui.activity

import android.view.View
import com.gyf.barlibrary.ImmersionBar
import com.hjq.dialog.WaitDialog
import com.hxd.charitysystem.R
import com.hxd.charitysystem.base.common.MyActivity
import com.hxd.charitysystem.bean.NetBaseBean
import com.hxd.charitysystem.handle.InputTextHelper
import com.hxd.charitysystem.utils.ProjectUrl
import com.hxd.tabactivityfragment.util.GsonToBean
import com.kymjs.rxvolley.RxVolley
import com.kymjs.rxvolley.client.HttpCallback
import com.kymjs.rxvolley.client.HttpParams
import kotlinx.android.synthetic.main.activity_register.*
import org.jetbrains.anko.startActivity

class RegisterActivity : MyActivity(), View.OnClickListener{

    private val registerWaitDialog by lazy { WaitDialog.Builder(this).setMessage("正在注册...").show(); }
    override fun getLayoutId(): Int {
        return R.layout.activity_register
    }

    override fun getTitleId(): Int {
        return R.id.tb_register_title
    }

    override fun initView() {
        //管理注册按钮是否可用
        InputTextHelper.Builder(this)
            .setMain(btn_register_commit)
            .addView(et_register_phone)
            .addView(et_register_code)
            .addView(et_register_password1)
            .addView(et_register_password2)
            .build()
        cv_register_countdown.setOnClickListener(this)
        btn_register_commit.setOnClickListener(this)
    }

    override fun statusBarConfig(): ImmersionBar {
        // 不要把整个布局顶上去
        return super.statusBarConfig().keyboardEnable(true)
    }

    override fun initData() {

    }

    override fun onClick(v: View?) {
        when(v?.id){
            //获取验证码的操作
            R.id.cv_register_countdown -> {
                //这里后期需要替换成验证手机号的工具
                if (et_register_phone.text.toString().length != 11) {
                    // 重置验证码倒计时控件
                    cv_register_countdown.resetState()
                    toast(getString(R.string.phone_input_error))
                } else {
                    // 获取验证码
                    toast(getString(R.string.countdown_code_send_succeed))
                }
            }

            //提交注册按钮的操作
            R.id.btn_register_commit -> {
                register()
            }
        }
    }

    /**
     * 注册逻辑
     */
    private fun register(){
        val account = et_register_phone.text.toString()
        val code = et_register_code.text.toString()
        val password1 = et_register_password1.text.toString()
        val password2 = et_register_password2.text.toString()
        if(password1 == password2){
            val params = HttpParams()
            params.put("account", account)
            params.put("password", password1)
            registerWaitDialog
            RxVolley.post(ProjectUrl.register, params, object:HttpCallback(){
                override fun onSuccess(result: String?) {
                    val data = GsonToBean.JsonToBean<NetBaseBean<String>>(result)
                    registerWaitDialog.dismiss()
                    if(data.status == 200){
                        toast("注册成功!")
                        startActivity<LoginActivity>()
                        finish()
                    }else{
                        toast(data.msg)
                    }
                }

                override fun onFailure(errorNo: Int, strMsg: String?) {
                    registerWaitDialog.dismiss()
                    toast(strMsg)
                }
            })
        }else{
            toast("两次输入的密码不一致，请重新输入!!!")
            clearEdit()
        }
    }

    private fun clearEdit(){
        et_register_password1.text.clear()
        et_register_password2.text.clear()
    }

}
